const app = require("./index");
const {
  SERVER_ERR,
  NAME_OR_PASSWORD_IS_REQUIRED,
  USER_IS_EXIST,
  USER_IS_NOT_EXIST,
  PASSWORD_IS_NOT_RIGHT,
  TAG_IS_EXIST,
  TEXT_IS_REQUIRED,
  TOKEN_IS_TIMEOUT,
} = require("../config/error.js");

app.on("error", (error, ctx) => {
  console.log("错误处理", error, ctx.error?.message);

  let code = 0;
  let message = "";

  switch (error) {
    case NAME_OR_PASSWORD_IS_REQUIRED:
      code = 10001;
      message = "用户名或者密码不能为空";
      break;
    case USER_IS_EXIST:
      code = 10002;
      message = "用户已存在";
      break;
    case TAG_IS_EXIST:
      code = 10003;
      message = "tag已存在";
      break;
    case TEXT_IS_REQUIRED:
      code = 10004;
      message = "text不能为空";
      break;
    case USER_IS_NOT_EXIST:
      code = 10005;
      message = "用户不存在";
      break;
    case PASSWORD_IS_NOT_RIGHT:
      code = 10006;
      message = "密码错误";
      break;
    case TOKEN_IS_TIMEOUT:
      code = 10007;
      message = "用户登录信息有误或已过期";
      break;
    case SERVER_ERR:
      // 通用报错
      code = 500;
      message = ctx.error.sql ? ctx.error : ctx.error.message;
  }

  ctx.body = { code, message };
});
